package com.qzsf.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qzsf.backend.common.Result;
import com.qzsf.backend.entity.Event;
import com.qzsf.backend.entity.Feedback;
import com.qzsf.backend.entity.Participation;
import com.qzsf.backend.entity.Registration;
import com.qzsf.backend.mapper.*;
import com.qzsf.backend.service.EventService;
import com.qzsf.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/11/17 21:52 周日
 * @Email 1429278462@qq.com
 * @Remark 活动服务层实现类
 * @Version 1.0
 */
@Service
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private EventTypeMapper eventTypeMapper;
    @Autowired
    private OrganizerProfileMapper organizerProfileMapper;
    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private ParticipationMapper participationMapper;
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public Result getEventType() {
        return Result.success(eventTypeMapper.getAllEventTypes());
    }

    @Override
    public Result getOrganizer() {
        return Result.success(organizerProfileMapper.getAllOrganizerDetail());
    }

    @Override
    public Result getEvents(Integer status, Integer typeId, Integer organizerId, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        //查询前先更新一下剩余人数
        eventMapper.updateRemaining();
        return Result.success(new PageInfo<>(eventMapper.getEventsByCondition(status,typeId,organizerId)));
    }

    @Override
    public Result getEventList() {
        eventMapper.updateRemaining();
        return Result.success(eventMapper.getEventList());
    }

    @Override
    public Result getEventById(int id) {
        eventMapper.updateRemaining();
        return Result.success(eventMapper.getEventById(id));
    }

    @Override
    public Result createEvent(Event event) {
        if(event.getPics() == null){
            event.setPics("[]");
        }
        if(eventMapper.createEvent(event)>0){
            return Result.success();
        }else{
            return Result.error("创建活动失败");
        }
    }

    @Override
    public Result updateEvent(Event event) {
        if(eventMapper.updateEvent(event)>0){
            return Result.success();
        }else {
            return Result.error("修改活动失败");
        }
    }

    @Override
    public Result deleteEvent(int id) {
        if(eventMapper.deleteEvent(id)>0){
            return Result.success();
        }else {
            return Result.error("删除活动失败");
        }
    }

    @Override
    public Result getEventsMobile(Integer typeId) {
        eventMapper.updateRemaining();
        return Result.success(eventMapper.getEventsByCondition(1,typeId,null));
    }

    @Override
    public Result getUserEvents() {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return Result.success(registrationMapper.getUserEvent(id));
    }

    @Override
    public Result getPastEvents() {
        return Result.success(eventMapper.getPastEvent());
    }

    @Override
    public Result getEventUserState(int eventId) {
        eventMapper.updateRemaining();
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        Event event = eventMapper.getEventById(eventId);
        Registration registration = registrationMapper.getRegistrationByUserAndEvent(id,eventId);
        Participation participation = participationMapper.getParticipationByUserAndEvent(id,eventId);
        Feedback feedback = feedbackMapper.getFeedbackByUserAndEvent(id,eventId);
        if(registration == null){
            if (event.getStartTime().isBefore(LocalDateTime.now())){
                return Result.success("event_ended_not_registered");
            }else {
                if(eventMapper.getRemainingByEventId(eventId)>0){
                    return Result.success("not_registered");
                }
                else{
                    return Result.success("registration_full");
                }
            }
        }else{
            int status = registration.getStatus();
            if(status == 1){
                if(participation == null){
                    return Result.success("reviewed_not_attended");
                }else{
                    if(feedback == null){
                        return Result.success("attended_not_scored");
                    }else{
                        return Result.success("attended_scored");
                    }
                }
            }
            else{
                return Result.success("registered_pending_review");
            }
        }
    }

    @Override
    public Result getEventDetailMobile(int eventId) {
        return Result.success(eventMapper.getEventDetailMobile(eventId));
    }

    @Override
    public Result createEventOrganizer(Event event) {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        event.setAuthId(id);
        if(event.getPics() == null){
            event.setPics("[]");
        }
        if(eventMapper.createEvent(event)>0){
            return Result.success();
        }else{
            return Result.error("创建活动失败");
        }
    }

    @Override
    public Result getOrganizerEvent(String query) {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        if(query == null){
            return Result.success(eventMapper.getOrganizerEvents(id));
        }else {
            return Result.success(eventMapper.getOrganizerEventsByQuery(id, query));
        }
    }

    @Override
    public Result getEventOrganizerState(int eventId) {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        Event event = eventMapper.getEventById(eventId);
        if(LocalDateTime.now().isBefore(event.getStartTime())){
            return Result.success("not_started");
        } else if (LocalDateTime.now().isAfter(event.getStartTime())&&LocalDateTime.now().isBefore(event.getEndTime())) {
            return Result.success("in_progress");
        }else{
            int registrationNum = registrationMapper.getEventRegistrationNum(eventId);
            int participationNum = participationMapper.getEventParticipationNum(eventId);
            if(participationNum<registrationNum){
                return Result.success("ended_not_feedback");
            }else{
                return Result.success("ended_with_feedback");
            }
        }
    }

    @Override
    public Result updateOrganizerEvent(Event event) {
        if(eventMapper.updateOrganizerEvent(event)>0){
            return Result.success();
        }else{
            return Result.error("修改当前活动失败");
        }
    }


}
